﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WxOpenPlatLibCore
{
    public class WXComm
    {
        public static string GetHtmlResponse(string url, string method = "GET", string postdata = null, string postcode = "utf-8", string xAuth = "")
        {
            try
            {
                if (!url.ToLower().StartsWith("http://") && !url.ToLower().StartsWith("https://"))
                {
                    url = "http://" + url;
                }
                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                req.Timeout = 10000;
                if (xAuth.Trim() != "")
                {
                    req.Headers.Add("X-Auth", xAuth);
                }
                req.Method = method.ToUpper();
                req.Headers.Add("XX-Device-Type", "wxweb");
                req.KeepAlive = false;
                if (req.Method == "POST" && !string.IsNullOrEmpty(postdata))
                {
                    req.ContentType = "application/x-www-form-urlencoded;";
                    byte[] buffer = System.Text.Encoding.GetEncoding(postcode).GetBytes(postdata);
                    req.ContentLength = buffer.Length;
                    using (System.IO.Stream reqStream = req.GetRequestStream())
                    {
                        reqStream.Write(buffer, 0, buffer.Length);
                        reqStream.Close();
                    }
                }

                System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
                string code = res.CharacterSet;
                System.Text.Encoding cod = System.Text.Encoding.Default;
                //if (code != null && code.ToUpper().StartsWith("UTF"))
                //{
                //    cod = System.Text.Encoding.UTF8;
                //}
                //else
                //{
                //    cod = System.Text.Encoding.Default;
                //}
                System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
                string result = sr.ReadToEnd();
                sr.Close();
                return result;
            }
            catch (Exception ex)
            {
                return "{\"errcode\":-1,\"errmsg\":\"调用接口错误\"}";
            }
        }

        public static void WriteLog(string msg)
        {
            try
            {
                msg = string.Format("{0}:{1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msg);
                System.IO.FileInfo fi = new System.IO.FileInfo(WXConfig.GetRootPath() + "/logs/log-" + DateTime.Now.ToString("yyyy_MM_dd") + ".txt");
                System.IO.FileStream fs = fi.OpenWrite();
                fs.Seek(fs.Length, System.IO.SeekOrigin.Begin);
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(msg);
                fs.Write(buffer, 0, buffer.Length);
                fs.Flush();
                fs.Close();
            }
            catch
            {

            }
        }
    }
}
